﻿using UnityEngine;
using System.Collections;

namespace UI
{
    public class UIFieldAskEaster : MonoBehaviour
    {
        public void Cancel()
        {
            FreeReborn();
        }

        public void Ok()
        {
            BuyReborn();
        }
        

        public string contentStr, timerTipStr;

        public UILabel content;
        public UILabel timeTip;

        const float fixedTimer = 10f;
        float timer = 0f;

        OkHander free, buy;
        int cost = 0;
        public void Init(OkHander free,OkHander buy)
        {
            contentStr = TableTextBlo.GetText("field_pvp_player_dead_tip");
            timerTipStr = TableTextBlo.GetText("field_pvp_easter_timer");

            timer = 0f;
            this.free = free;
            this.buy = buy;

            var str = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.FieldBuyLife;
            var sss = str.Split(new char[1] { ',' }, System.StringSplitOptions.RemoveEmptyEntries);
            cost = int.Parse(sss[1]);
            content.text = string.Format(contentStr, TableTextBlo.GetText(sss[0]), sss[1]);

            Show();
        }

        void Update()
        {
            timer += Time.deltaTime;
            timeTip.text = string.Format(timerTipStr, (int)(fixedTimer - timer));
            if (timer >= fixedTimer)
            {
                FreeReborn();
            }

            CheckPlayerState();
        }

        void CheckPlayerState()
        {
            var room =Room.RoomManager.currentRoom as Room.RoomField;
            if(room != null)
            {
                var self = room.charactorManager.GetSelfCharactor();
                if(self!=null)
                {
                    var ai = self.ai as Room.RCAIActor;
                    if(ai!=null)
                    {
                        var state = ai.curState;
                        if(state != Room.PlayerAIState.Death)
                        {
                            Hide();
                        }
                    }
                }   
            }
        }

        void FreeReborn()
        {
            free();
            Hide();
        }

        void BuyReborn()
        {
            if(StaticData.playerData.diamond >= cost)
            {
                buy();
                Hide();
            }
            else
            {
                BoxManager.CreatePopupTextBox(TableTextBlo.GetText("diamond_lack"));
            }
        }

        void Show()
        {
            gameObject.SetActive(true);
        }

        void Hide()
        {
            gameObject.SetActive(false);
        }
    }
}